using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Collections.Generic;

namespace ImageReview
{
    public partial class ImageList : System.Web.UI.UserControl
    {
        public string FolderToLoad = String.Empty;
        public string ImageFilemask = String.Empty;
        public List<FileInfo> fileInfoList = new List<FileInfo>();

        protected void Page_Load(object sender, EventArgs e)
        {
            PlaceHolder phListItems = (PlaceHolder)this.FindControl("phListItems");
            DirectoryInfo directoryInfo = new DirectoryInfo(FolderToLoad);
            if (directoryInfo.Exists)
            {
                //create a list of all existing images in imagesets
                ArrayList existingImages = new ArrayList();
                List<TPDSControl.ImageSet> existingImagesets = TPDSControl.ImageSet.LoadAll(directoryInfo.FullName);
                foreach (TPDSControl.ImageSet imgset in existingImagesets)
                {
                    foreach (TPDSControl.Image img in imgset.Images)
                    {
                        existingImages.Add(Path.GetFileName(img.Filename).ToUpper());
                    }
                }

                ImageListItem imageListItem = null;
                
                foreach (FileInfo fi in directoryInfo.GetFiles(ImageFilemask))
                {
                    if ((!fi.Name.ToUpper().Contains("_THUMB")) && (!existingImages.Contains(fi.Name.ToUpper())))
                    {
                        fileInfoList.Add(fi);
                    }
                }

                fileInfoList.Sort(new TPDSConfig.Utils.FileInfoComparer()); //sort files by date and name

                foreach (FileInfo fi in fileInfoList)
                {
                    imageListItem = (ImageListItem)this.LoadControl("ImageListItem.ascx");
                    imageListItem.Filename = fi.FullName;
                    phListItems.Controls.Add(imageListItem);
                }

                //Panel submitPanel = (Panel) this.Parent.FindControl("submitPanel");
                //if (fileInfoList.Count == 0)
                //    submitPanel.Visible = false;
                //else
                //    submitPanel.Visible = true;
            }
        }
    }
}